//@ sourceURL=asd.js
/*$(function(){ 
	$("#addBtn").click(function(){
    	//$("#title").text("添加菜单");
        $("#content").load(getPath() + "base/menu/add");
    });
}); */

/*window.onload=function(){ 
	$("#addBtn").click(function(){
    	$("#title").text("添加菜单");
        $("#content").load(getPath() + "base/menu/add");
    });
} */
//菜单树
var menu_setting = {
    check:{
        enable:false
//			chkStyle:"checkbox",
//			chkboxType:{"Y":"s","N":"s"}
    },
    data:{
        simpleData:{
            enable:true
        }
    },
    view:{
        showLine:false,
        dblClickExpand:true,
        fontCss:setFontCss_ztree
    },
    callback:{
        onClick:menuzTreeOnClick
    }
};

//菜单父类树
var menuSort_setting = {
		check:{
			enable:false
//			chkStyle:"radio"
//			chkboxType:{"Y":"s","N":"s"}
		},
		data:{
			simpleData:{
				enable:true
			}
		},
		view:{
			showLine:false,
			dblClickExpand:true,
			fontCss:setFontCss_ztree
		},
		callback: {
	    	
	    }
};

//查看权限树
var lookAuthority_setting = {
	check: {
        enable: false
        //chkboxType:  { "Y" : "", "N" : "" }
        //chkStyle: "radio"
    },
    view: {
        dblClickExpand: false,
        fontCss:setFontCss_ztree
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
    	
    }
};

//操作权限树
var operateAuthority_setting = {
		check: {
			enable: true,
			chkboxType:  { "Y" : "", "N" : "" }
			//chkStyle: "radio"
		},
		view: {
			dblClickExpand:false,
			fontCss:setFontCss_ztree
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
	    	
	    }
};

var zTree,editzTree,addMenuzTree,authorityEditTree;
var menuName, menuId;//保存选择的菜单名跟id值
var loadDiv = null;
$(function () {
	
	setTimeout(function(){
	    $(".grid-list").load(getPath() + "base/menu/add");
	}, 100);
	
	loadDiv = $("#loadDiv").loading();//初始化提示操作执行中
    //自动获取树控件高度
	setZTreeHeight();//autoZTreeHeight();
    //初始化获取所有菜单
    initGetMenu();
    //初始化树
    initTree();
    
    $("#title").text("菜单管理");
    //去除所有控件的虚线框
    removeBlur();
    
    
    
});

//获取所有菜单
function initGetMenu(){
	$.post(
		getPath() + "base/menu/tree",{parentId:0},
		 function(data){
			 var treeNodes = data;
			 result = eval("("+treeNodes+")");
			 if(result.isSuccess){
				 resultObj = result.resultObject;
				 resultObj.splice(0,1);
				 menuzTree = $.fn.zTree.init($("#menuTree"),menu_setting,resultObj);
			 }
	 });
}

//初始化树
function initTree(){
	//父菜单树（增删改共用同一棵）
	post(getPath() + "base/menu/tree",{parentId:0},function(treeNodes){
		$.fn.zTree.init($("#parentMenuTree"),menuSort_setting,treeNodes);  
	});
	//菜单查看权限树（增删改共用同一棵）
	post(getPath() + "base/authority/tree/getMenuLookAuthorityTree",{},function(treeNodes){
		$.fn.zTree.init($("#lookAuthorityMenuTree"),lookAuthority_setting,treeNodes);
	});
	/*//菜单操作权限树（增删改共用同一棵）
	post(getPath() + "base/authority/tree",{pid:0},function(treeNodes){
		treeNodes.splice(0,1);//去掉第一个节点
		$.fn.zTree.init($("#operateAuthorityMenuTree"),operateAuthority_setting,treeNodes); 
	});*/
}

//删除
function deleteMenu(){
    var selectedArray = menuzTree.getSelectedNodes();
    var promptWindow = null;
    if(selectedArray.length == 0){
        promptWindow = createWindow('请选择需要删除的菜单！');
    }else{
        promptWindow = createWindow('你确定要删除该菜单？',{
            cancleEnable:true,
            callBack:function(){
                var ids = '';
                for(var i=0;i<selectedArray.length;i++){
                    if(ids.length == 0){
                        ids += selectedArray[i].id;
                    }else{
                        ids += ","+selectedArray[i].id;
                    }
                }
                loadDiv.showLoading();
                post(getPath() + "base/menu/delete/" + ids,{},
                    function(result){
                		loadDiv.hideLoading();
                        promptWindow.dialog("close");
                        var parentMenuTree = $.fn.zTree.getZTreeObj('parentMenuTree');
                        var menuTree = $.fn.zTree.getZTreeObj('menuTree');
                        for(var i=0;i<selectedArray.length;i++){
                            var parentMenuNode = parentMenuTree.getNodeByParam("id",selectedArray[i].id);
                            var menuNode =  menuTree.getNodeByParam("id",selectedArray[i].id);
                            parentMenuTree.removeNode(parentMenuNode);
                            menuTree.removeNode(menuNode);
                        }
                        $(".grid-list").html("");
                        $("#title").text("菜单管理");
                        menuName=null;
                        menuId=null;
                        //删除成功，弹出删除成功提示框，当用户点击确定后刷新页面
                        promptWindow = createWindow("删除成功！");
                        /*promptWindow.dialog("open");*/
                        $(".grid-list").load(getPath() + "base/menu/add");
                    },function(resultObject){
                    	loadDiv.hideLoading();
                    	$(".grid-list").load(getPath() + "base/menu/add");
    	        		/*createWindow(resultObject).dialog('open');*/
    	        	});
            }
        });
    }
    promptWindow.dialog('open');
}

//菜单树的点击事件
function menuzTreeOnClick(event,treeId,treeNode){
    $("#title").text("编辑菜单");
    $(".grid-list").load(getPath() + "base/menu/edit/" + treeNode.id);
    //保存选择的菜单名跟id
    menuName = treeNode.name;
    menuId = treeNode.id;
}

function parentMenuzTreeOnClick(event,treeId,treeNode){
	$("#menuParentName").val(treeNode.name);
	if(treeNode.id == -1){
		$("#parentId").val(0);
	}else{
		$("#parentId").val(treeNode.id);
	}
}

/**
 * 获取指定id节点的数据
 * @param treeId 父节点
 * @param id id
 * @returns {*}
 */
function getNodeById(treeId, id){
    var zTree = $.fn.zTree.getZTreeObj(treeId);
    var targetNode = zTree.getNodesByFilter(function (node){
        return node.id == id;
    },true);

    return targetNode;
}

//操作成功后的弹窗
function openDialog(tipInfo, src){
	var promptWindow = $("<div style='overflow-y: hiddern;'><p style='margin: 5px;'>"+tipInfo+"</p></div>");
    promptWindow.appendTo("html");
    promptWindow.dialog({
        autoOpen: false,
        width: 200,
        height: 130,
        resizable: false,
        title: "系统提示",
        modal: true,
        buttons: {
            "确定": function () {
                window.location.href = getPath() + src;
            }
        }
    });
    return promptWindow;
}


